#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
using LL = long long;

struct Edge
{
    int a, b, w;
    bool f;
    bool operator< (const Edge& W)const{
        return w < W.w;
    }
};

const int N = 510, M = 1e4 + 10;

int n, m;
int h[N], e[2 * N], ne[2 * N], w[2 * N], idx;
Edge edge[M];
int dist1[N][N], dist2[N][N];
int p[N];

int find(int x){
    if(x != p[x]){
        p[x] = find(p[x]);
    }

    return p[x];
}

void add(int a, int b, int c){
    e[idx] = b;
    ne[idx] = h[a];
    w[idx] = c;
    h[a] = idx ++;
}

void dfs(int u, int fa, int max1, int max2, int d1[], int d2[]){
    d1[u] = max1, d2[u] = max2;

    for(int k = h[u]; k != -1; k = ne[k]){
        int j = e[k];
        if(j != fa){
            int t1 = max1, t2 = max2;
            if(w[k] > t1){
                t2 = t1, t1 = w[k];
                dfs(j, u, t1, t2, d1, d2);
            }else if(w[k] > t2 && w[k] < t1){
                t2 = w[k];
                dfs(j, u, t1, t2, d1, d2);
            }else{
                dfs(j, u, t1, t2, d1, d2);
            }
        }
    }
}

int main(){
    LL sum = 0;
    LL res = 1e18;

    memset(h, -1, sizeof h);

    scanf("%d%d", &n, &m);

    for(int i = 0; i < m; i ++){
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        edge[i].a = x;
        edge[i].b = y;
        edge[i].f = false;
        edge[i].w = z;
    }

    sort(edge, edge + m);
    for(int i = 1; i <= n; i ++) p[i] = i;

    for(int i = 0; i < m; i ++){
        int x = edge[i].a, y = edge[i].b, z= edge[i].w;
        int a = find(x), b = find(y);

        if(a != b){
            p[a] = p[b];
            sum = (LL) sum + z;
            edge[i].f = true;
            add(x, y, z);
            add(y, x, z);
        }
    }

    for(int i = 1; i <= n; i ++)  dfs(i, -1, -1e9, -1e9, dist1[i], dist2[i]);

    for(int i = 0; i < m; i ++){
        if(!edge[i].f){
            int x = edge[i].a, y = edge[i].b, z = edge[i].w;

            LL t;
            if (z > dist1[x][y])
                t = sum + z - dist1[x][y];
            else if (z > dist2[x][y])
                t = sum + z - dist2[x][y];
            res = min(res, t);
        }
    }
    printf("%lld\n", res);

    return 0;
}